<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/21 0021
 * Time: 11:10
 */

namespace app\api\controller;

use think\Controller;
use think\Request;

use app\api\model\Member;
use app\api\model\MemberTmp;
use app\api\model\Token;
use app\api\model\Base;

use app\api\validate\CheckValidate;

/**
 * 登陆相关
 * Class Login
 * @package app\api\controller
 */

class Login extends Controller
{

    

   /**
    * 会员注册
    * @param Member $model
    * @return \think\response\Json
    */
    public function reg(){
        $model = new Member; 
        if(!$this->request->isPost()){
            return apiError('请求方式错误'); 
        }
        $request = $this->request->post();
        if(empty($request['tel'])) return apiError('手机号缺失');
        $tel = isset($request['tel']) ? $request['tel'] : '';
        $code = isset($request['code']) ? $request['code'] : '';
        $pwd = isset($request['pwd']) ? $request['pwd'] : '';
        $pwd1 = isset($request['pwd1']) ? $request['pwd1'] : '';
        $member_info = $model->_reg($tel, $code, $pwd, $pwd1 );
        if(!$member_info){
            return apiError($model->getError());
        }
        return apiSuccess($member_info);
    }
    
    /**
     * 会员登陆
     * @param Member $model
     * @return \think\response\Json
     */
    public function index(){
        $model = new Member; 
        if(!$this->request->isPost()){
            return apiError('请求方式错误'); 
        }
        $request = $this->request->post();

        $validate = new CheckValidate();
        if(!$validate->scene($validate::SCENE_LOGIN_TEL)->check($request)){
            return apiError($validate->getError());
        }
        
        $tel = $request['tel'];
        $pwd = $request['pwd'];
        $member_info = $model->_login($tel, $pwd);
        if(!$member_info){
            return apiError($model->getError());
        }
        return apiSuccess($member_info);
    }
    
    
    /**
    * 忘记密码
    */
    public function forget(){
        $model = new Member; 
        if(!$this->request->isPost()){
            return apiError('请求方式错误'); 
        }
        $request = $this->request->post();

        $validate = new CheckValidate();
        if(!$validate->scene($validate::SCENE_FORGET)->check($request)){
            return apiError($validate->getError());
        }
        $tel = $request['tel'];
        $pwd = $request['pwd'];
        $code = $request['code'];
        $member_info = $model->_forget($tel, $code, $pwd);
        if(!$member_info){
            return apiError($model->getError());
        }
        return apiSuccess([],'成功');
    }


    /**
     * 退出登陆
     * @param Member $model
     * @return \think\response\Json
     */
    public function logout(){
        $model = new Token;
        $token = $this->request->post('token');
        $member_token = $model->_getTokenByToken($token);
        if(empty($member_token)){
            return apiError('token异常');
        }
        $model->token = '';
        $model->deadline = 0;
        $model->save();
        return apiSuccess();
    }
    
    /**
    * 分享下载
    */
    public function ureg(){
        $request = $this->request->get();
        
        $param = base64_encode(serialize($request));
        //var_dump($param);die;
        $this->assign([
            'incode' => $param
        ]);
        
        return $this->fetch();
    }
    
}